package com.uni.service.impl;

import com.uni.dao.ProductMapper;
import com.uni.dao.ProductProviderMapper;
import com.uni.dao.ProviderMapper;
import com.uni.pojo.ProductProviderExample;
import com.uni.pojo.Provider;
import com.uni.pojo.ProviderExample;
import com.uni.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @作者: Unirithe
 * @日期: 2022/2/21
 **/

@Service
public class ProviderServiceImpl implements ProviderService {
    @Autowired
    public ProviderMapper providerMapper;
    @Autowired
    public ProductProviderMapper productProviderMapper;
    @Autowired
    public ProductMapper productMapper;

    @Override
    public List<Provider> getAll() {
        return providerMapper.selectByExample(new ProviderExample());
    }

    @Override
    public Provider getByProductId(Long id) {
        return providerMapper.selectByPrimaryKey(id);
    }

    @Override
    public Provider getByProviderId(Long prodId) {
        return providerMapper.selectByPrimaryKey(prodId);
    }

    @Override
    public Provider autoModifyById(Provider provider) {
        return providerMapper.updateByPrimaryKeySelective(provider) == 1 ? provider : null;
    }
//分界线
    @Override
    public List<Provider> getBySome(Map<String, String> paramMap) {
        return providerMapper.selectBySome(paramMap);
    }

    @Override
    public int addProvider(Provider provider) {
        return providerMapper.insertSelective(provider);
    }

    @Override
    public int removeProviderByIds(List<Long> ids) {
        // 先删除供应商对应的商品
        int i = productMapper.deleteProductByProviderIds(ids);
        // 再删除供应商
        i += providerMapper.deleteProviderByIds(ids);
        return i;
    }
}
